Skip to content

Fix slice filter appending fill_with when items divide evenly#2150

Closed
Krishnachaitanyakc wants to merge 2 commits intopallets:mainfrom
Krishnachaitanyakc:fix-slice-fill-with-evenly-divisible
Closed

Fix slice filter appending fill_with when items divide evenly#2150
Krishnachaitanyakc wants to merge 2 commits intopallets:mainfrom
Krishnachaitanyakc:fix-slice-fill-with-evenly-divisible

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown

Summary

  • Fixes the slice filter incorrectly appending fill_with to every slice when the iterable length is evenly divisible by the slice count.
  • Root cause: When length % slices == 0, slices_with_extra is 0, making the condition slice_number >= slices_with_extra always True. This causes fill_with to be appended to every slice even though no padding is needed.
  • Fix: Add a truthiness check for slices_with_extra before the comparison, so fill_with is only appended when there are actually shorter slices that need padding.

Before (broken):

>>> [1,2,3,4]|slice(4, 'foo')|list
[[1, 'foo'], [2, 'foo'], [3, 'foo'], [4, 'foo']]

After (fixed):

>>> [1,2,3,4]|slice(4, 'foo')|list
[[1], [2], [3], [4]]

Fixes #2118

Test plan

  • Added test_slice_evenly_divisible covering evenly divisible cases (4 items / 4 slices, 6 items / 2 slices)
  • Verified existing test_slice still passes (non-evenly-divisible case with fill_with works correctly)
  • All 133 sync filter tests and 2 async slice tests pass

Krishna Chaitanya Balusu and others added 2 commits March 24, 2026 22:30
When the iterable length is evenly divisible by the slice count,
`slices_with_extra` is 0. The condition `slice_number >= slices_with_extra`
is then always true, causing `fill_with` to be incorrectly appended to
every slice. Add a truthiness check for `slices_with_extra` to ensure
`fill_with` is only appended when there are actually shorter slices
that need padding.

Fixes pallets#2118
@davidism davidism closed this Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slice returns one extra item when slice count is a divisor of iterable length and fill_with not none

2 participants